package com.kwok.rbac.application.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwok.rbac.application.common.security.SystemUserTokenThreadLocal;
import com.kwok.rbac.application.common.utils.R;
import com.kwok.rbac.application.entity.Employee;
import com.kwok.rbac.application.entity.SystemUserToken;
import com.kwok.rbac.application.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-05-24
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 添加单个员工
     * @param employee
     * @return
     */
    @PostMapping("/save")
    public R saveEmp(@RequestBody Employee employee){
        return R.write(employeeService.save(employee));
    }

    /**
     * 批量保存员工
     * @param employeeList
     * @return
     */
    @PostMapping("/batchSave")
    public R batchSaveEmp(@RequestBody List<Employee> employeeList){
        return R.write(employeeService.saveBatch(employeeList));
    }

    /**
     * 根据主键查询单个员工
     * @param eNumber
     * @return
     */
    @GetMapping("/get/{eNumber}")
    public Employee getEmp(@PathVariable Integer eNumber){
        Employee employee = employeeService.getById(eNumber);
        log.debug("查询单个员工信息:{}",employee);
        return employee;
    }

    /**
     * 查询所有员工
     * @return
     */
    @GetMapping("/list")
    public List<Employee> empList(){
        List<Employee> employeeList = employeeService.list();
        log.debug("查询所有员工信息:{}",employeeList);
        return employeeList;
    }

    /**
     * 员工分页查询
     * @param page
     * @return
     */
    @PostMapping("/empPage")
    public Page<Employee> empPage(@RequestBody Page<Employee> page){
        SystemUserToken systemUserToken = SystemUserTokenThreadLocal.get();
        log.debug("系统用户的Token:{}",systemUserToken);
        //MP提供的分页
        //Page<Employee> employeePage = employeeService.page(page);
        //自定义分页
        Page<Employee> employeePage = employeeService.employeePage(page);
        log.debug("员工分页查询:{}",employeePage);
        return employeePage;
    }

    /**
     * 根据主键修改员工信息
     * @param employee
     * @return
     */
    @PutMapping("/update")
    public R updateEmp(@RequestBody Employee employee){
        return R.write(employeeService.updateById(employee));
    }

    /**
     * 根据主键删除单个员工（接口待修改，为了配合前端项目的删除和批量删除功能）
     * @param eNumber
     * @return
     */
    @DeleteMapping("/{eNumber}")
    public R removeEmp(@PathVariable Integer eNumber){
        return R.write(employeeService.removeById(eNumber));
    }

    /**
     * 根据主键批量删除员工（接口待修改，为了配合前端项目的删除和批量删除功能）
     * @param eNumbers
     * @return
     */
    @DeleteMapping
    public R batchRemoveEmp(@RequestBody List<Integer> eNumbers){
        return R.write(employeeService.removeByIds(eNumbers));
    }

}
